// $codepro.audit.disable numericLiterals
/**
 * CREATE USER PANEL
 * This class is used to create a Nurse, Doctor, or System Admin
 * 
 */


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.ButtonGroup;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


/**
 * 
 * 
 * Create User constructor
 *@author Team Awesome Express
 *
 * @version $Revision: 1.0 $
 */
public class CreateUser extends JPanel {
	/**
	 * Field txtUserName.
	 */
	private final JTextField txtUserName;
	
	/**
	 * Field lbUserName
	 */
	private final JLabel lbErrUserName;

	/**
	 * Field txtFirstName.
	 */
	private final JTextField txtFirstName;
	
	/**
	 * Field lbFirstName
	 */
	private final JLabel lbErrFirstName;

	/**
	 * Field txtLastName.
	 */
	private final JTextField txtLastName;
	
	/**
	 * Field lbLastName
	 */
	private final JLabel lbErrLastName;

	/**
	 * Field buttonGroup.
	 */
	private final ButtonGroup buttonGroup = new ButtonGroup();

	/**
	 * Field myDataManager.
	 */
	private DataManager myDataManager;

	/**
	 * Field txtPassword.
	 */
	private final JTextField txtPassword;
	
	/**
	 * Field lbPassword
	 */
	private final JLabel lbErrPassword;

	/**
	 * Field userType.
	 */
	private String userType;

	/**
	 * Field frame.
	 */
	private static JFrame Frame;

	/**
	 * Create the panel.
	 * @param myDataManager DataManager
	 */
	public CreateUser(final DataManager myDataManager) {
		setBackground(GlobalVars.BACKGROUND);
		this.myDataManager = myDataManager;
		setLayout(null);
		
		txtUserName = new JTextField();
		txtUserName.setBounds(84, 11, 86, 20);
		add(txtUserName);
		txtUserName.setColumns(10);
		
		lbErrUserName = new JLabel();
		lbErrUserName.setBounds(175, 11, 150, 20);
		lbErrUserName.setForeground(Color.RED);
		add(lbErrUserName);
		
		txtPassword = new JTextField();
		txtPassword.setBounds(84, 42, 86, 20);
		add(txtPassword);
		txtPassword.setColumns(10);
		
		lbErrPassword = new JLabel();
		lbErrPassword.setBounds(175, 42, 150, 20);
		lbErrPassword.setForeground(Color.RED);
		add(lbErrPassword);
		
		txtFirstName = new JTextField();
		txtFirstName.setBounds(84, 73, 86, 20);
		add(txtFirstName);
		txtFirstName.setColumns(10);
		
		lbErrFirstName = new JLabel();
		lbErrFirstName.setBounds(175, 73, 150, 20);
		lbErrFirstName.setForeground(Color.RED);
		add(lbErrFirstName);
		
		txtLastName = new JTextField();
		txtLastName.setBounds(84, 114, 86, 20);
		add(txtLastName);
		txtLastName.setColumns(10);
		
		lbErrLastName = new JLabel();
		lbErrLastName.setBounds(175, 114, 150, 20);
		lbErrLastName.setForeground(Color.RED);
		add(lbErrLastName);
		
		final JRadioButton rdbtnNurse = new JRadioButton("Nurse");
		rdbtnNurse.setSelected(true);
		buttonGroup.add(rdbtnNurse);
		rdbtnNurse.setBounds(42, 175, 109, 23);
		add(rdbtnNurse);
		
		final JRadioButton rdbtnDoctor = new JRadioButton("Doctor");
		buttonGroup.add(rdbtnDoctor);
		rdbtnDoctor.setBounds(184, 175, 109, 23);
		add(rdbtnDoctor);
		
		final JRadioButton rdbtnSystem = new JRadioButton("System Admin");
		buttonGroup.add(rdbtnSystem);
		rdbtnSystem.setBounds(318, 175, 109, 23);
		add(rdbtnSystem);
		
		final JLabel lblUsername = new JLabel("Username :");
		lblUsername.setBounds(10, 14, 64, 14);
		add(lblUsername);
		
		final JLabel lblFirstName = new JLabel("First Name :");
		lblFirstName.setBounds(10, 76, 64, 14);
		add(lblFirstName);
		
		final JLabel lblLastName = new JLabel("Last Name :");
		lblLastName.setBounds(10, 117, 64, 14);
		add(lblLastName);
		
		final JButton btnCreate = new JButton("Create");
		
		btnCreate.addMouseListener(new MouseAdapter() {
		
			public void mousePressed(MouseEvent arg0) { // If the create button is pressed
			
				final DataIntegrity dataCheck = new DataIntegrity();
				final String[] userInfo = new String[3];
				boolean foundErr = false;
				
				lbErrUserName.setVisible(false);
				lbErrPassword.setVisible(false);
				lbErrFirstName.setVisible(false);
				lbErrLastName.setVisible(false);
				
				// Default is Nurse
				if (rdbtnNurse.isSelected()) {
					userType = "Nurse";
				}
				
				else if (rdbtnDoctor.isSelected()) {
					userType = "Doctor";
				}
				
				else if (rdbtnSystem.isSelected()) {
					userType = "System Admin";
				}   
				
				/**
				 * Data Integrity Check
				 */
				//text box empty check
				if(txtUserName.getText().equals("")){
					foundErr = true;
					lbErrUserName.setText("Missing required information");
					lbErrUserName.setVisible(true);
				}
				if(txtPassword.getText().equals("")){
					foundErr = true;
					lbErrPassword.setText("Missing required information");
					lbErrPassword.setVisible(true);
				}
				if(txtFirstName.getText().equals("")){
					foundErr = true;
					lbErrFirstName.setText("Missing required information");
					lbErrFirstName.setVisible(true);
				}
				if(txtLastName.getText().equals("")){
					foundErr = true;
					lbErrLastName.setText("Missing required information");
					lbErrLastName.setVisible(true);
				}
				
				userInfo[0] = txtUserName.getText();
				userInfo[1] = txtFirstName.getText();
				userInfo[2] = txtLastName.getText();
				
				//User ID Check (Duplicate ID)
				final String[] id = myDataManager.getDataAtCol(GlobalVars.USER_SHEET, "Username", "Username");
				for(int i=0; i < id.length; i++){
					if(id[i].equals(userInfo[0])){
						foundErr = true;
						lbErrUserName.setText("Duplicated ID");
						lbErrUserName.setVisible(true);
					}
				}
				
				//Last Name (Letter only)
				if(!dataCheck.isOnlyLetters(userInfo[2])){
					foundErr = true;
					lbErrLastName.setText("Verify entered information");
					lbErrLastName.setVisible(true);
				}
				
				//First Name (Letter only)
				if(!dataCheck.isOnlyLetters(userInfo[1])){
					foundErr = true;
					lbErrFirstName.setText("Verify entered information");
					lbErrFirstName.setVisible(true);
				}
				
				if(!foundErr){
					myDataManager.createUserData(txtUserName.getText(), txtPassword.getText(), userType, "Unlocked", "0"); // Create the user
					
						if (userType.equals("Nurse")) {
							myDataManager.setData(GlobalVars.NURSE_SHEET, null, userInfo, -1);
						}
						else if (userType.equals("Doctor")) {
							myDataManager.setData(GlobalVars.DOCTOR_SHEET, null, userInfo, -1);
						}
						else if (userType.equals("System Admin")) {
							myDataManager.setData(GlobalVars.ADMIN_SHEET, null, userInfo, -1);
						}
					Frame.dispose();
				}
			}
				
		});
		btnCreate.setBounds(177, 215, 89, 23);
		add(btnCreate);
		
		final JLabel lblNewLabel = new JLabel("Password :");
		lblNewLabel.setBounds(10, 39, 64, 14);
		add(lblNewLabel);
	}
	
	/**
	 * Method createWindow.
	 * @param myDataManager DataManager
	 * @return JFrame
	 */
	public static JFrame createWindow(DataManager myDataManager) {
		Frame = new JFrame();
		Frame.setSize(450, 350);
		Frame.getContentPane().add(new CreateUser(myDataManager));
		Frame.setVisible(true);
		Frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		return Frame;
		}
	
//    public String getUserTypeSelected() {
//		if (rdbtnNurse.isSelected()) {
//			userType = "Nurse";
//		}
//		
//		else if (rdbtnDoctor.isSelected()) {
//			userType = "Doctor";
//		}
//		
//		else if (rdbtnSystem.isSelected()) {
//			userType = "System Admin";
//		}   
//        
//        return userType;
//    }
}
